{extend name="base/common"}

{block name="style"}
<link rel="stylesheet" href="/static/datepicker/bootstrap-datetimepicker.min.css">
<link rel="stylesheet" href="/static/iCheck/custom.css">
<link rel="stylesheet" href="/admin/css/chosen/chosen.css">
<style>
    .imggroup{
        position: relative;
    }
    .imgbox{
        height: 100px;
        width: 200px;
        margin-top: 5px;
        margin-bottom: 0;
    }
    .imgfile{
        height: 100px;
        width: 200px;
        position: absolute;
        top:0;
        opacity: 0;
    }
    .plus{
        height: 100px;
        width: 200px;
        font-size: 100px;
        line-height: 100px;
        text-align: center;
        color: rgba(204, 204, 204, 0.51);
    }
    .plus:hover{
        font-size: 150px;
    }
    .plus2{
        display: none;
    }
    .plusimg{
        display: none;
    }
</style>
{/block}

{block name="page-header"}
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>购物商城</h2>
        <ol class="breadcrumb">
            <li>
                <a href="{:Url('Shopping/index')}">订单列表</a>
            </li>
            <li class="active">
                {empty name="msg"}
                <strong>添加</strong>
                {else/}
                <strong>修改</strong>
                {/empty}
            </li>
        </ol>
    </div>
    <div class="col-lg-2">

    </div>
</div>
{/block}

{block name="main"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5></h5>
                    <div class="ibox-tools">
                        <a href="javascript:window.history.go(-1);" class="btn btn-white btn-sm"><i class="fa fa-reply"></i> 返回</a>
                    </div>
                </div>
                <div class="ibox-content all">
                    <form id="form" class="form-horizontal" method="post">
                        <div class="form-group">
                            <div class="col-lg-1 text-right control-label">
                                <label>封面图片:</label>
                            </div>
                            {eq name="msg" value=""}
                            <div class="col-lg-5">
                                <div class="imggroup">
                                    <img src="" alt='图片' class='thumbnail imgbox'>
                                    <input type="file" class="imgfile" accept="image/jpg, image/png, image/gif, image/jpeg">
                                    <input type="hidden" name="front_cover" class="img"  value="" required="">
                                </div>
                                <b>推荐尺寸：750*400px</b>
                            </div>
                            {else/}
                            <div class="col-lg-5">
                                <div class="imggroup">
                                    <img src="{$msg.front_cover|get_cover='path'}" alt='图片' class='thumbnail imgbox'>
                                    <input type="file" class="imgfile" accept="image/jpg, image/png, image/gif, image/jpeg">
                                    <input type="hidden" name="front_cover" class="img"  value="{$msg.front_cover}" required="">
                                </div>
                                <b>推荐尺寸：750*400px</b>
                            </div>
                            {/eq}
                       </div>
                        <div class="form-group">
                            <div class="col-lg-1 text-right control-label">
                                <label>标题:</label>
                            </div>
                            <div class="col-lg-11">
                                <input type="text" placeholder="请输入标题" class="form-control form_title" name="title" value="{$msg.title || default=''}">
                            </div>
                        </div>
                        <!--<div class="form-group">-->
                            <!--<div class="col-lg-1 text-right control-label">-->
                                <!--<label>简介:</label>-->
                            <!--</div>-->
                            <!--<div class="col-lg-11">-->
                                <!--<div class=" float-e-margins">-->
                                    <!--<div class="ibox-content no-padding borders">-->
                                        <!--<textarea  placeholder="请输入简介..." class="text form-control form_title" name="description" >{$msg.description || default=''}</textarea>-->
                                    <!--</div>-->
                                <!--</div>-->
                            <!--</div>-->
                        <!--</div>-->
                        <div class="form-group">
                            <div class="col-lg-1 control-label">
                                <label>内容:</label>
                            </div>
                            <div class="col-lg-11">
                                <div class=" float-e-margins">
                                    <div class="ibox-content no-padding borders">
                                        <script id="container" name="content" type="text/plain">{$msg.content || default=''}</script>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-1 text-right control-label">
                                <label>发布人:</label>
                            </div>
                            <div class="col-lg-11">
                                <input type="text" placeholder="请输入发布人" class="form-control form_title" name="publisher" value="{$msg.publisher || default=''}">
                            </div>
                        </div>

                        <input type="hidden" name="id" value="{$msg.id || default=''}">
                        <div class="form-group">
                            <div class="col-lg-11 col-lg-offset-1">
                                <button class="btn btn-primary submit-btn post-put" type="button" target-form="form-horizontal" >发布</button>
                                <a class="btn btn-default" href="javascript:window.history.go(-1);">返回</a>
                            </div>
                        </div>
                        <div class="selblo"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script src="/static/uploadify/jquery.uploadify.min.js"></script>
<script src="/static/iCheck/icheck.min.js"></script>
<script src="/static/ueditor/ueditor.config.js"></script>
<script src="/static/ueditor/ueditor.all.min.js"></script>
<script src="/admin/js/chosen.jquery.js"></script>
<script>
    $(function(){
        //    单选按钮
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });

        highlight_subnav("{:Url('Order/index')}");
        //上传图片
        var plus = $('.plus');
        var imgnum = 1;
        plus.on('click',function(){
            var it = $(this);
            it.hide().next().fadeIn();
            imgnum = 3;
            if(!it.hasClass('plus2')){
                plus.eq(1).fadeIn();
                imgnum = 2
            }
        });
        $(".imgfile").on("change",function(){
            var size = ($(this)[0].files[0].size / 1024).toFixed(2);
            var imgBox = $(this);
            if(size <= 2048){
                var img = $(this)[0].files[0];
                var formData = new FormData();
                var pic=$(this);
                formData.append("picture",img);
                $.ajax({
                    type:"post",
                    url:"{:Url('File/uploadPicture')}",
                    data:formData,
                    processData : false,
                    contentType : false,
                    success:function(msg){
                        var msg = $.parseJSON(msg);
                        var path = msg.data.path;
                        if(msg['code'] == 1){
                            imgBox.siblings(".img").val(msg.data["id"]);
                            //显示图片缩略图
                            pic.parent().find("img" ).attr({src:path});
                        } else {
                            warning({msg:msg['msg']});
                            return;
                        }
                    }
                });
            } else {
                warning({msg:"您选择的图片超过2mb，请重新选择"});
                return;
            }
        });

        var ue = UE.getEditor('container',{
            initialFrameHeight:400
        });

        //发布
        $(".post-put").off('click').on("click",function(){
            var it=$(this);
            var data = {
                id:$("input[name='id']").val(),
//                type:$("select[name='type']").val(),
                front_cover:$("input[name='front_cover']").val(),
                title:$("input[name='title']").val(),
//                description:$("textarea[name='description']").val(),
                content:ue.getContent(),
                publisher:$("input[name='publisher']").val(),
//                status:0,
            };
            $.ajax({
                type:"post",
                url:$("#form").attr("action"),
                data:data,
                success:function(data){
                    if(data.code == 1){
                        if (data.url) {
                            updateAlert(data.msg + ' 页面即将自动跳转~','success');
                        }else{
                            updateAlert(data.msg ,'success');
                        }
                        it.prop('disabled','true');
                        it.prev().prop('disabled','true');
                        setTimeout(function(){
                            if (data.url) {
                                location.href=data.url;
                            } else {
                                location.reload();
                            }
                            it.removeAttr('disabled');
                            it.prev().removeAttr('disabled')
                        },2000);
                    } else {
                        updateAlert(data.msg, 'error');
                        it.attr('disabled','true');
                        it.prev().prop('disabled','true');
                        setTimeout(function(){
                            it.removeAttr('disabled');
                            it.prev().removeAttr('disabled')
                        },2000);
                    }
                }
            });
            return false;
        });
    })
    $("select" ).chosen({
        no_results_text: "没有找到该选项",
        allow_single_de: true,
        width:'100%',
        max_selected_options:'100'
    });
</script>
{/block}